package com.mioto.ld.module.product.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.mioto.ld.module.product.model.ProductAttributeEditDTO;
import com.mioto.ld.component.EditType;
import org.springframework.stereotype.Service;

import com.mioto.ld.module.product.dao.ProductAttributeDao;
import com.mioto.ld.module.product.model.ProductAttribute;
import com.mioto.ld.module.product.service.IProductAttributeService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author mioto
 * @date 2024-05-14 21:05:43
 */
@Service("productAttributeService")
public class ProductAttributeServiceImpl implements IProductAttributeService{

    @Resource
    private ProductAttributeDao productAttributeDao;

    @Override
    public List<ProductAttribute> findList(ProductAttribute productAttribute) {
        return productAttributeDao.findList(productAttribute);
    }

    @Override
    public int insert(ProductAttribute productAttribute) {
        productAttribute.initCreateData();
        return productAttributeDao.insertIgnoreNull(productAttribute);
    }

    @Override
    public int update(ProductAttribute productAttribute) {
        return productAttributeDao.update(productAttribute);
    }

    @Override
    public int updateIgnoreNull(ProductAttribute productAttribute) {
        return productAttributeDao.updateIgnoreNull(productAttribute);
    }

    @Override
    public ProductAttribute findByColumn(String column, Object value) {
        return productAttributeDao.findByColumn(column,value);
    }

    @Override
    public int deleteByColumn(String column, Object value) {
        return productAttributeDao.deleteByColumn(column,value);
    }

    @Override
    public int batchDelete(Integer[] ids) {
        return productAttributeDao.batchDelete(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int edit(List<ProductAttributeEditDTO> productAttributes) {
        int result = 0;
        if (CollUtil.isNotEmpty(productAttributes)){
            result = 1;
            productAttributes.forEach(p->{
                if (p.getEditType() == EditType.EDIT_TYPE_INSERT){
                    insert(p);
                }else if (p.getEditType() == EditType.EDIT_TYPE_UPDATE){
                    updateIgnoreNull(p);
                }else if (p.getEditType() == EditType.EDIT_TYPE_DELETE){
                    deleteByColumn("id",p.getId());
                }
            });
        }
        return result;
    }

    @Override
    public List<ProductAttribute> findListBySn(String sn) {
        return productAttributeDao.findListBySn(sn);
    }

    @Override
    public Map<Integer,String> findIdAndIdentityByProductIdAndAttr(Integer productId, String[] attrNames) {
        List<ProductAttribute> list = productAttributeDao.findListByProductIdAndAttr(productId,attrNames);
        Map<Integer,String> map = new HashMap<>(attrNames.length);
        if (CollUtil.isNotEmpty(list)) {
            list.forEach(productAttribute -> {
                map.put(productAttribute.getId(),productAttribute.getIdentification());
            });
        }
        return map;
    }
}